package com.jicheng.service.impl;

import com.jicheng.Utils.MD5Util;
import com.jicheng.constants.Constants;
import com.jicheng.dto.app.PermissionParams;
import com.jicheng.dto.sysUser.*;
import com.jicheng.entity.SysRole;
import com.jicheng.entity.SysUser;
import com.jicheng.ext.dto.SearchPagingParams;
import com.jicheng.ext.util.BeanConvertor;
import com.jicheng.framework.utils.JsonUtils;
import com.jicheng.mapper.SysUserMapper;
import com.jicheng.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: SysUserServiceImpl
 * @Description: TODO
 * @author: khz
 * @date: 2020年09月16日 15:45
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public String getPassword(String username) {
        return sysUserMapper.getPassword(username);
    }

    @Override
    public String getRole(String username) {
        return null;
    }

    @Override
    public void updatePassword(String userId, String newPassword) {
        sysUserMapper.updatePassword(userId, newPassword);
    }

    @Override
    public List<String> getUser() {
        return null;
    }

    @Override
    public List<RolePermiDto> getRolePermission(SearchPagingParams searchPagingParams) {
        return sysUserMapper.getRolePermission(searchPagingParams);
    }

    @Override
    public String getPermission(String username) {
        return null;
    }

    @Override
    public void addUser(SysUserParams sysUser) {
        SysUser copy = BeanConvertor.copy(sysUser, SysUser.class);
        sysUserMapper.insertSelective(copy);
    }

    @Override
    public void addRole(SysRoleParams sysRole) {
        sysUserMapper.addRole(sysRole);
    }

    @Override
    public List<RoleDto> getRoleList() {
        return sysUserMapper.getRoleList();
    }

    @Override
    public List<SysUserDto> userList(SearchPagingParams searchPagingParams) {
        List<SysUserDto> list = sysUserMapper.userList(searchPagingParams);

        return list;
    }

    @Transactional
    @Override
    public void setPermisson(RolePermiParams rolePermiParams) {

        sysUserMapper.delRolePermis(rolePermiParams.getRoleId());
        List<PermissionParams> permission = rolePermiParams.getPermission();
        for (PermissionParams permissionParams : permission) {
            rolePermiParams.setPermissionId(permissionParams.getPermissionId());
            for (Integer childrenId : permissionParams.getChildrenId()) {
                rolePermiParams.setChildrenId(childrenId);
                sysUserMapper.setPermisson(rolePermiParams);
            }
        }
    }

    @Override
    public List<PermiforRoleDto> getPermiforRole(String roleId) {
        //通过角色id获取一级权限菜单
        List<PermiforRoleDto> permiforRole = sysUserMapper.getPermiforRole(roleId);
        for (PermiforRoleDto permiforRoleDto : permiforRole) {
            //通过角色id和一级权限id组合查询拥有二级权限
            List<ChildrenPermisDto> children = sysUserMapper.getPermiforChildren(permiforRoleDto.getId(), Integer.parseInt(roleId));
            permiforRoleDto.setChildren(children);
        }

        Iterator<PermiforRoleDto> it = permiforRole.iterator();
        while(it.hasNext()){
            PermiforRoleDto x = it.next();
            if(x.getChildren().size()==0){
                it.remove();
            }
        }
        return permiforRole;


    }

    @Override
    public void updateRole(String userId, String roleId) {
        sysUserMapper.updateRole(userId, roleId);
    }

    @Transactional
    @Override
    public void delUser(int userId) {
        //接触用户绑定的角色，即删除用户角色关系
        sysUserMapper.delRoleUser(userId);
        //删除用户
        sysUserMapper.delUser(userId);
    }


    @Override
    public  UserInfo getUserInfo(String username) {
        UserInfo userInfo = sysUserMapper.getUserInfo(username);

        //通过用户名获取一级权限
        List<Menu> menus = sysUserMapper.getMenu(username);
        //倒序
        Collections.reverse(menus);
        for (Menu menu : menus) {

                //通过角色id和一级权限id组合查询拥有二级权限
                List<ChildrenPermisDto> children = sysUserMapper.getPermiforChildren(menu.getId(), Integer.parseInt(String.valueOf(menu.getRoleId())));
                menu.setChildren(children);

        }
        List<PermiforRoleDto> allPermission = sysUserMapper.getAllPermission();
        for (PermiforRoleDto children : allPermission) {

            List<ChildrenPermisDto> chil = sysUserMapper.getPermiforChildrenAll(children.getId());
            children.setChildren(chil);
        }
        userInfo.setAll(allPermission);
        userInfo.setMenu(menus);
        return userInfo;
    }

    @Override
    public void resetPass(String userId) {
        String password = MD5Util.shiroEncryption(Constants.DEFAULT_PASSWORD);
        sysUserMapper.resetPass(userId, password);
    }


    @Transactional
    @Override
    public void delRole(int roleId) {
        //解除用户角色关系
        sysUserMapper.delUserRole(roleId);
        //解除角色权限关系
        sysUserMapper.delRolePermis(roleId);
        //删除角色
        sysUserMapper.delRole(roleId);
    }

    @Override
    public void addPermission(SysPermiParams sysPermiParams) {
        sysUserMapper.addPermission(sysPermiParams);
    }
}
